package com.rms.hkpc.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.rms.hkpc.R;
import com.rms.hkpc.activity.base.BaseFragmentActivity;
import com.rms.hkpc.constant.AllConstants;
import com.rms.hkpc.dialog.ResultDialog;
import com.rms.hkpc.util.DateUtil;

import java.util.Calendar;
import java.util.Date;

public class AddHocOtherActivity extends BaseFragmentActivity {

    private final String WAT_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private Calendar calendar,calendarWAt;
    private EditText etVal;
    private TextView tvExp, tvResult, tvWAt;
    private int result;
    private Integer val;
    private String expAt,wAt;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_hoc_other;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvExp = fd(R.id.tv_exp);
        etVal = fd(R.id.et_val);
        tvResult = fd(R.id.tv_result);
        tvWAt = fd(R.id.tv_w_at);
        MyOnClickListener onClickListener = new MyOnClickListener();
        tvWAt.setOnClickListener(onClickListener);
        tvExp.setOnClickListener(onClickListener);
        tvResult.setOnClickListener(onClickListener);
        fd(R.id.iv_close).setOnClickListener(onClickListener);
        fd(R.id.tv_ok).setOnClickListener(onClickListener);
        initData();
    }

    private void initData() {
        Intent get = getIntent();
        result = get.getIntExtra(AllConstants.RESULT, 1);
        val = get.getIntExtra(AllConstants.VALUE, 0);
        expAt = get.getStringExtra(AllConstants.EXP);
        wAt = get.getStringExtra(AllConstants.WAT);
        calendar = Calendar.getInstance();
        if (!TextUtils.isEmpty(expAt)) {
            calendar.setTime(DateUtil.getInstance().StrToDateTime(expAt));
            tvExp.setText(expAt.substring(0, 10));
        }
        calendarWAt = Calendar.getInstance();
        if (!TextUtils.isEmpty(wAt)) {
            calendarWAt.setTime(DateUtil.getInstance().StrToDateTime(wAt,WAT_FORMAT));
            tvWAt.setText(wAt);
        }
        etVal.setText(val == 0 ? "" : String.valueOf(val));
        tvResult.setText(result == 1?R.string.success:R.string.fail);
    }

    @Override
    protected void initFragment() {

    }

    class MyOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tv_w_at:
                    DateUtil.getInstance().showDateSelector(AddHocOtherActivity.this, v, calendarWAt,WAT_FORMAT, new DateUtil.ShowTimeSelectorCallback() {
                        @Override
                        public void OnTimeSelectListener(Date date) {
                            calendarWAt.setTime(date);
                            wAt = DateUtil.getInstance().DateTimeToStr(date,WAT_FORMAT);
                            tvWAt.setText(wAt);
                        }
                    });
                    break;
                case R.id.iv_close:
                    finish();
                    break;
                case R.id.tv_ok:
                    if (TextUtils.isEmpty(wAt)) {
                        Toast.makeText(AddHocOtherActivity.this,R.string.p_sel_time,Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (!TextUtils.isEmpty(etVal.getText().toString())) {
                        try {
                            val = Integer.parseInt(etVal.getText().toString());
                        } catch (NumberFormatException e) {
                            Toast.makeText(AddHocOtherActivity.this,R.string.val_error,Toast.LENGTH_SHORT).show();
                            e.printStackTrace();
                            return;
                        }
                    }
                    Intent intent = new Intent();
                    intent.putExtra(AllConstants.RESULT,result);
                    intent.putExtra(AllConstants.EXP,expAt);
                    intent.putExtra(AllConstants.VALUE,val);
                    intent.putExtra(AllConstants.WAT,wAt);
                    setResult(1,intent);
                    finish();
                    break;
                case R.id.tv_result:
                    new ResultDialog(AddHocOtherActivity.this, result, new ResultDialog.IDoneListener() {
                        @Override
                        public void getDoneFromDialog(int str, int resultId) {
                            tvResult.setText(str);
                            result = resultId;
                        }
                    }).show();
                    break;
                case R.id.tv_exp:
                    DateUtil.getInstance().showDateSelector(AddHocOtherActivity.this, v, calendar, new DateUtil.ShowTimeSelectorCallback() {
                        @Override
                        public void OnTimeSelectListener(Date date) {
                            calendar.setTime(date);
                            expAt = DateUtil.getInstance().DateTimeToStr(date,"yyyy-MM-dd");
                            tvExp.setText(expAt);
                        }
                    });
                    break;
            }
        }
    }
}